# To run an R script and save logs appropriately
RDOCKER=mkdir -p logs && docker run --rm -it -v .:/tmp/replication structwar Rscript --verbose --no-save --no-restore --no-site-file --no-environ $(1) > logs/$(1:.r=.log) 2>&1

# Numbered R scripts
fit_base_r=01_fit_base.r
boot_base_r=02_boot_base.r
fit_postwar_r=03_fit_postwar.r
boot_postwar_r=04_boot_postwar.r
fit_joiners_r=05_fit_joiners.r
boot_joiners_r=06_boot_joiners.r
regression_tables_r=07_regression_tables.r
force_multipliers_r=08_force_multipliers.r
war_probabilities_r=09_war_probabilities.r
coalitions_r=10_coalitions.r
model_comparison_r=11_model_comparison.r
descriptives_r=12_descriptives.r
equilibrium_illustrations_r=13_equilibrium_illustrations.r
remake_data_r=OPTIONAL_remake_data.r


all : \
	figures/figure_1a.pdf \
	figures/figure_1b.pdf \
	figures/figure_2.pdf \
	figures/figure_3.pdf \
	figures/figure_A1.pdf \
	figures/figure_A2.pdf \
	figures/figure_A3a.pdf \
	figures/figure_A3b.pdf \
	tables/table_1.tex \
	tables/table_2a.tex \
	tables/table_2b.tex \
	tables/table_2c.tex \
	tables/table_3.tex \
	tables/table_4a.tex \
	tables/table_4b.tex \
	tables/table_5a.tex \
	tables/table_5b.tex \
	tables/table_6.tex \
	tables/table_A1.tex \
	tables/table_A2.tex \
	tables/table_A3.tex \
	tables/table_A4.tex \
	tables/table_A5.tex \
	tables/table_A6.tex \
	tables/table_A7a.tex \
	tables/table_A7b.tex \
	tables/table_A8.tex \
	tables/table_A9.tex

figures/figure_1a.pdf figures/figure_1b.pdf :
	$(call RDOCKER,$(equilibrium_illustrations_r))

tables/table_A1.tex tables/table_A2.tex :
	$(call RDOCKER,$(descriptives_r))

tables/table_A8.tex : results/fit_base.rda results/boot_base.rda
	$(call RDOCKER,$(model_comparison_r))

figures/figure_2.pdf figures/figure_3.pdf figures/figure_A2.pdf figures/figure_A3a.pdf figures/figure_A3b.pdf tables/table_5a.tex tables/table_5b.tex tables/table_6.tex tables/table_A5.tex tables/table_A6.tex tables/table_A7a.tex tables/table_A7b.tex : results/fit_base.rda
	$(call RDOCKER,$(coalitions_r))

tables/table_4a.tex tables/table_4b.tex : results/fit_base.rda
	$(call RDOCKER,$(war_probabilities_r))

figures/figure_A1.pdf tables/table_1.tex tables/table_2a.tex tables/table_2b.tex tables/table_2c.tex tables/table_A3.tex : results/fit_base.rda
	$(call RDOCKER,$(force_multipliers_r))

tables/table_3.tex tables/table_A4.tex tables/table_A9.tex : results/fit_base.rda results/fit_joiners.rda results/fit_postwar.rda results/boot_base.rda results/boot_joiners.rda results/boot_postwar.rda
	$(call RDOCKER,$(regression_tables_r))

results/boot_joiners.rda : results/fit_joiners.rda results/boot_index_joiners.rda
	$(call RDOCKER,$(boot_joiners_r))

results/fit_joiners.rda results/boot_index_joiners.rda :
	$(call RDOCKER,$(fit_joiners_r))

results/boot_postwar.rda : results/fit_postwar.rda results/boot_index_postwar.rda
	$(call RDOCKER,$(boot_postwar_r))

results/fit_postwar.rda results/boot_index_postwar.rda :
	$(call RDOCKER,$(fit_postwar_r))

results/boot_base.rda : results/fit_base.rda results/boot_index_base.rda
	$(call RDOCKER,$(boot_base_r))

results/fit_base.rda results/boot_index_base.rda :
	$(call RDOCKER,$(fit_base_r))


# Optionally rebuild data
generated_dispute.rda generated_participant.rda generated_state_year generated_dyad_year.rda :
	$(call RDOCKER,$(remake_data_r))
